package utils;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

import views.SelectedView;
import data.ShowData;

public class CompressHandler extends AbstractHandler
{

    public CompressHandler()
    {
        // TODO Auto-generated constructor stub
    }

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException
    {
        StructuredSelection sels = (StructuredSelection) SelectedView
                .getInstance().getSelection();
        for (Iterator iterator = sels.iterator(); iterator.hasNext();)
        {
            ShowData data = (ShowData) iterator.next();
            String path = data.getFileName();
            String newPath = path.substring(0, path.lastIndexOf("."))
                    + ".compress";
            File f = new File(newPath);
            if (f.exists())
            {
                boolean answer = MessageDialog.openQuestion(HandlerUtil
                        .getActiveShell(event), "Overwrite", "compress file for\n"
                        + newPath + "\nalready exists, do you want to delete?");
                if (answer)
                {
                    f.delete();
                }
            } else
            {
                try
                {
                    f.createNewFile();
                } catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}
